package com.thinkgem.jeesite.common.utils;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import com.thinkgem.jeesite.modules.config.entity.message.ConfigMessage;
import com.thinkgem.jeesite.modules.config.service.message.ConfigMessageService;

public class CellPhoneMessage {
    
  //  private ConfigMessageService service;
    
//    private static String URL="http://gw.api.taobao.com/router/rest";
//    private String appkey="23631690";

    public void sendPhoneMessage(String tel,String code) throws Exception{
  
        ConfigMessageService configMessageService =SpringContextHolder.getBean(ConfigMessageService.class);
        ConfigMessage ms = configMessageService.getMessageConfig();
        if(ms!=null){
            String json= "{\"code\":\"" + code + "\"}";//"{code:"+code+",company:某某电子商务验证}";
            TaobaoClient client = new DefaultTaobaoClient(ms.getUrl(),ms.getAppKey(), ms.getSecret());
            AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
            req.setExtend("123456");
            req.setSmsType("normal");
            req.setSmsFreeSignName(ms.getSignName());
            req.setSmsParamString(json);
            
            req.setRecNum(tel);
            req.setSmsTemplateCode(ms.getTemplateCode());
            
            try {
                AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req); 
                if(rsp.getSubCode()!=null){
                    if(rsp.getSubCode().equals("isv.OUT_OF_SERVICE")){
                        throw new Exception("业务停机,请登陆www.alidayu.com充值");
                    }
                    if(rsp.getSubCode().equals("isv.AMOUNT_NOT_ENOUGH")){
                        throw new Exception("因余额不足未能发送成功，请登录管理中心充值后重新发送");
                    }
                    if(rsp.getSubCode().equals("isv.SMS_SIGNATURE_ILLEGAL")){
                        throw new Exception("签名不合法");
                    }
                    if(rsp.getSubCode().equals("isv.MOBILE_NUMBER_ILLEGAL")){
                        throw new Exception("手机号码格式错误   ,使用合法的手机号码");
                    }
                }
            } catch (ApiException e) {
               
                e.printStackTrace();
            }  
        }
       
    }
}
